<?php

/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-01-20
 * 版    本：1.0.0
 * 功能说明：用户控制器。
 *
 * */

namespace Qwadmin\Controller;

//use Common\Controller\openerp;

class PickingController extends ComController {
    /*
     * 商品列表
     */

    function __construct() {
        parent::__construct();
    }

    public function index() {

       $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $m = M('stock_picking');
        $sh['pick_id'] = I('get.pick_id');
        $sh['supplier'] = I('get.supplier');
        $sh['buyer_name'] = I('get.buyer_name');
        $sh['trade_pattern'] = I('get.trade_pattern');
        $sh['ebpCode'] = I('get.ebpCode');
        $where = array();

        if ($sh['buyer_name'] && !empty($sh['buyer_name'])) {
            $where['buyer_name'] = array('like', $sh['buyer_name']);
        }
        if ($sh['pick_id'] && !empty($sh['pick_id'])) {
            $where['pick_id'] = array('eq', $sh['pick_id']);
        }
        if ($sh['channel_code'] && !empty($sh['channel_code'])) {
            $where['channel_code'] = array('eq', $sh['channel_code']);
        }
        if ($sh['trade_pattern'] && !empty($sh['trade_pattern'])) {
            $where['trade_pattern'] = array('eq', $sh['trade_pattern']);
        }
        if ($sh['ebpCode'] && !empty($sh['ebpCode'])) {
            $where['ebpCode'] = array('eq', $sh['ebpCode']);
        }


        $pagesize = 20; #每页数量
        $offset = $pagesize * ($p - 1); //计算记录偏移量
        $count = $m->where($where)->count();
        $list = $m->where($where)->limit($offset . ',' . $pagesize)->order("date desc")->select();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $supplierlist = $m->field("pick_type")->group('pick_type')->cache(200)->select();
        $operlist = $m->field("oper")->group('oper')->cache(204)->select();
        $trade_typelist = $m->field("location_id")->group('location_id')->cache(204)->select();
        $this->assign('list', $list);
        $this->assign('channel_codelist', $channel_codelist);
        $this->assign('trade_patternlist', $trade_patternlist);
        $this->assign('ebpCodelist', $ebpCodelist);
        $this->assign('page', $page);
        $this->assign('sh', $sh);
        $this->display();
    }

    /***
     * 
     * 订单详情
     */
    function detail(){
        $ref=I("get.poname","0");
        if(!$ref){
            $this->error("参数错误",U("index"));
        }
        $m=D("purchorder");
        $res=$m->where(array('poname'=>$ref))->find();
//        dump($res);
        $this->assign("val",$res);
        $this->display();
    }
    /*
     * 商品添加
     */

    public function add() {

        $goods_brand = M('Goods_brand')->field('id,name')->select();
        $goods_types = M('Goods_type')->field('id,name,pid')->select();
        $country = M('country')->where(array('enabled' => 'true'))->field('country_id,Chinese_name')->select();

        $tradeList = M("Trade")->group('code')->select();


        $tree = new \Tree\Tree(M('goods_type')->field('id,pid,name')->select());
        $tree->icon = array('&nbsp;&nbsp;&nbsp;&nbsp;|', '&nbsp;&nbsp;&nbsp;&nbsp;|------------', '&nbsp;&nbsp;&nbsp;&nbsp;|-');

        $this->assign('select_option_list', $tree->get_tree(0, '<option value=\$id \$selected>\$spacer\$name</option>'));
        $this->assign('country', $country);
        $this->assign('goods_brand', $goods_brand);
        $this->assign('tradeList', $tradeList);

        $this->display('form');
    }

    public function update() {
        $m = M('goods');
        $id = I('post.id', '', 'intval');
        $data = I('post.arr');
        $pic_thumb = I('post.pic_thumb', '', 'strip_tags');

        $data['pic_thumb'] = $pic_thumb ? $pic_thumb : '';
        if ($data['info_keywords']) {
            $data['info_keywords'] = json_encode(array_unique(explode("、", $data['info_keywords'])));
        }

        if ($id) {
            $m->data($data)->where("id='{$id}'")->save();
            addlog('编辑商品，ID：' . $id);
        } else {
            $m->data($data)->add();
            addlog('新增商品，名称：' . $data['name']);
        }

        $this->success('操作成功！', U('index'));
    }

    /*
     * 商品编辑
     */

    public function edit($id = 0) {
        $id = intval($id);

        $m = M('goods');
        $goods = $m->where("id='$id'")->find();
        if (!$goods) {
            $this->error('参数错误！');
        }
        $goods['info_keywords'] = implode('、', json_decode($goods['info_keywords']));
        $goods_brand = M('Goods_brand')->field('id,name')->select();
        $goods_types = M('Goods_type')->field('id,name,pid')->select();
        $country = M('country')->where(array('enabled' => 'true'))->field('country_id,Chinese_name')->select();

        $tradeList = M("Trade")->group('code')->select();
        $tree = new \Tree\Tree(M('goods_type')->field('id,pid,name')->select());
        $tree->icon = array('&nbsp;&nbsp;&nbsp;&nbsp;|', '&nbsp;&nbsp;&nbsp;&nbsp;|------------', '&nbsp;&nbsp;&nbsp;&nbsp;|-');
        $this->assign('select_option_list', $tree->get_tree(0, '<option value=\$id \$selected>\$spacer\$name</option>'));
        $this->assign('tradeList', $tradeList);
        $this->assign('goods_brand', $goods_brand);

        $this->assign('country', $country);
        $this->assign('goods', $goods);
        $this->display('form');
    }

    public function del() {
        $ids = isset($_REQUEST['ids']) ? $_REQUEST['ids'] : false;

        if (is_array($ids)) {
            foreach ($ids as $k => $v) {
                $ids[$k] = intval($v);
            }
            if (!$ids) {
                $this->error('参数错误！');
                $uids = implode(',', $uids);
            }
        }

        $map['id'] = array('in', $ids);
        if (M('goods')->where($map)->delete()) {
            addlog('删除商品ID：' . $ids);
            $this->success('恭喜，商品删除成功！');
        } else {
            $this->error('参数错误！');
        }
    }

    /*
     * 商品的批处理相关
     */

    function goodsBath() {
        $ids = I("post.ids");
        $siteids = I("post.siteids");
        $storeids = I("post.storeids", 0);
        $optype = I("post.optype", ADD_NEW);
//        var_dump($optype);die;
        $jumpUrl = $_SERVER['HTTP_REFERER'];
        if (empty($ids) || !$ids) {
            $this->error("商品为空，请选择要操作的商品");
        }
        if (empty($siteids) || !$siteids) {
            $this->error("站点为空，请选择要操作的站点");
        }
//        if (empty($storeids) || !$storeids) {
//            $this->error("店铺为空，请选择要操作的店铺");
//        }

        $data = array();
        foreach ($ids as $key => $goods_id) {
            foreach ($siteids as $sk => $svalue) {
                $tempdata['goods_id'] = $goods_id;
                $tempdata['site_id'] = $svalue;
                $tempdata['op'] = $optype;
                $tempdata['uid'] = $this->USER['uid'];
                $tempdata['store_id'] = $storeids;
                $tempdata['optime'] = time();
                $data[] = $tempdata;
            }
        }
//        var_dump($data);die;
        $opModel = M("Goods_op");
        $opModel->startTrans();
        foreach ($data as $goodsdata) {
            $where['goods_id'] = $goodsdata['goods_id'];
            $where['site_id'] = $goodsdata['site_id'];
            $where['op'] = $goodsdata['op'];
//            $where['store_id'] = $goodsdata['store_id'];
            $hasid = $opModel->where($where)->getField("id");
            if ($hasid) {
                $updata = $goodsdata;
                $updata['rsp'] = '';
                $flag = $opModel->where(array("id" => $hasid))->save($updata);
            } else {
                $flag = $opModel->add($goodsdata);
            }

            if (!$flag) {
                $opModel->rollback();
                $this->error("操作失败，请重试", $jumpUrl);
            }
        }
        $opModel->commit();
        $this->success("操作成功", $jumpUrl);
    }

}
